namespace ComponentGo.Calendars 
{
	/// <summary>
	/// Collection of calendar breaks.
	/// </summary>
	public class BreakCollection: System.Collections.ArrayList
	{
		private System.Collections.ArrayList mBreaks;
 
		/// <summary>
		/// Costructor.
		/// </summary>
		public BreakCollection()
		{
			mBreaks = this;
		}

		/// <summary>
		/// Add break to collection.
		/// </summary>
		public void Add(Break b)
		{
			if (b.BeginBreak!=b.EndBreak)
				mBreaks.Add(b);
		}

		/// <summary>
		/// Remove break from collection.
		/// </summary>
		public void Remove(Break b)
		{
			mBreaks.Remove(b);
		}

		/// <summary>
		/// Size of collection.
		/// </summary>
		public int Size()
		{
			return mBreaks.Count; 
		}

		
		/// <summary>
		/// Is specified time in breaks of this collection.
		/// </summary>
		public bool IsExtraBreak(System.TimeSpan  dateBegin, System.TimeSpan dateEnd)
		{
			System.TimeSpan d = System.TimeSpan.Zero;
			for(int i = 0; i<mBreaks.Count; i++)
			{
				Break b = (Break)mBreaks[i];
				if(b.Extra)
				{
						if(b.BeginBreak<=dateBegin)
						{
							if(dateEnd<=b.EndBreak)
								return true;
							else
								if(dateBegin<b.EndBreak && d<=b.EndBreak)
								return true; 
						}
				}
			}
			return false;
		}

		/// <summary>
		/// Is specified time in breaks of this collection.
		/// </summary>
		public System.TimeSpan IsBreak(System.TimeSpan  dateBegin, System.TimeSpan dateEnd)
		{
			System.TimeSpan d = System.TimeSpan.Zero;
			for(int i = 0; i<mBreaks.Count; i++)
			{
				Break b = (Break)mBreaks[i];

					if(b.BeginBreak<=dateBegin)
					{
						if(dateEnd<=b.EndBreak)
							return dateEnd;
						else
							if(dateBegin<b.EndBreak && d<=b.EndBreak)
							d = b.EndBreak; 
					}
			}
			if(d != System.TimeSpan.Zero) return d;
			return dateBegin;
		}

		/// <summary>
		/// Get break by index.
		/// </summary>
		public Break GetByIndex(int i)
		{
			if(i>=Size())
				return null;
			return (Break)mBreaks[i];
		}
	}
}